package com.caisejingyu.whales.common.utils

import android.content.Context
import android.content.SharedPreferences
import com.caisejingyu.whales.common.BaseApplication

/**
 * @author: xuxiaojie
 */

object SPUtil {

    val defaultSP = BaseApplication.getAppContext().getSharedPreferences("defaultSP", Context.MODE_PRIVATE)

    fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        return defaultSP.getBoolean(key, defaultValue)
    }

    fun putBoolean(key: String, value: Boolean) {
        defaultSP.edit().putBoolean(key, value).apply()
    }

    fun getString(key: String, defaultValue: String?): String? {
        return defaultSP.getString(key, defaultValue)
    }

    fun getString(key: String): String? {
        return defaultSP.getString(key, null)
    }

    fun putString(key: String, value: String?) {
        defaultSP.edit().putString(key, value).apply()
    }

    fun getInt(key: String, defaultValue: Int): Int {
        return defaultSP.getInt(key, defaultValue)
    }

    fun putInt(key: String, value: Int) {
        defaultSP.edit().putInt(key, value).apply()
    }

    fun getLong(key: String, defaultValue: Long): Long {
        return defaultSP.getLong(key, defaultValue)
    }

    fun putLong(key: String, value: Long?) {
        value?.let { defaultSP.edit().putLong(key, value).apply() }
    }

    fun getFloat(key: String, defaultValue: Float): Float {
        return defaultSP.getFloat(key, defaultValue)
    }

    fun putFloat(key: String, value: Float?) {
        value?.let { defaultSP.edit().putFloat(key, value).apply() }
    }

    fun removeKey(key: String) {
        defaultSP.edit().remove(key).apply()
    }

    fun clear() {
        defaultSP.edit().clear().apply()
    }


}